package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerInfo;
import com.woniu.entity.FlowerType;
import com.woniu.mapper.FlowerInfoMapper;
import com.woniu.mapper.FlowerTypeMapper;
import com.woniu.service.FlowerInfoService;
import com.woniu.vo.FlowerInfoVo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
public class FlowerInfoServiceImpl implements FlowerInfoService {
    @Resource
    private FlowerInfoMapper flowerInfoMapper;
    
    @Resource
    private FlowerTypeMapper flowerTypeMapper;

    @Override
    //@Cacheable(value = "flowerInfo", key = "#root.methodName")
    public PageInfo<FlowerInfoVo> selectAllFlowerInfo(Integer pageNum, Integer pageSize,String flowerName,Long startTime,Long endTime,Integer isDelete) {
        PageHelper.startPage(pageNum,pageSize);
        
        if (startTime != null && endTime != null) {
            Date startTimedate = new Date(startTime);
            Date endTimedate = new Date(endTime);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTimedate1 = simpleDateFormat.format(startTimedate).substring(0,10)+" 00:00:00";

            String endTimedate1 = simpleDateFormat.format(endTimedate).substring(0,10)+" 23:59:59";
            List<FlowerInfoVo> list = flowerInfoMapper.selectAllFlowerInfo(flowerName,startTimedate1,endTimedate1,isDelete);
            return new PageInfo<FlowerInfoVo>(list);
        }
        
        List<FlowerInfoVo> list = flowerInfoMapper.selectAllFlowerInfo(flowerName,null,null,isDelete);
        PageInfo<FlowerInfoVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
   // @CacheEvict(value = "flowerInfo", allEntries = true)
    public int updateFlowerInfoByFlowerIdToStatus(FlowerInfo flowerInfo) {
        return flowerInfoMapper.updateByPrimaryKeySelective(flowerInfo);
    }

    @Override
    
    public List<FlowerInfo> selectAllFlowerInfoByIsDelete(FlowerInfo flowerInfo) {
        Example example = new Example(FlowerInfo.class);
        if (flowerInfo.getIsDelete()!=1){
            example.createCriteria().andEqualTo("isDelete", flowerInfo.getIsDelete());
        }else{
            example.createCriteria().andNotEqualTo("isDelete", flowerInfo.getIsDelete());
        }
        return flowerInfoMapper.selectByExample(example);
    }

    @Override
   // @CacheEvict(value = "flowerInfo", allEntries = true)
    public int addFlowerInfo(FlowerInfo flowerInfo) {
        return flowerInfoMapper.insertSelective(flowerInfo);
    }

    @Override
   // @CacheEvict(value = "flowerInfo", allEntries = true)
    public int updateFlowerInfo(FlowerInfo flowerInfo) {
        return  flowerInfoMapper.updateByPrimaryKeySelective(flowerInfo);
    }

    @Override
   // @Cacheable(value = "flowerType", key = "#root.methodName")
    public List<FlowerType> selectAllFlowerType() {
        return flowerTypeMapper.selectAll();
    }
}
